package com.zmn.uac.client;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.uac.UacException;
import com.zmn.uuc.common.constant.DubboConsts;
import com.zmn.uuc.common.dro.pass.PassIdentityBaseDRO;
import com.zmn.uuc.dubbo.interfaces.common.TokenRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @title: 登录token Dubbo接口
 * @author:Rananquan
 * @Date:2022/6/9 15:21
 */
@Slf4j
@Component
public class TokenClient {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    TokenRemoteService tokenRemoteService;

    /**
     * 登录token获取登录用户身份信息
     * @param token 登录token
     * @return
     */
    public List<PassIdentityBaseDRO> listPassIdentityByToken(String token){
        ResponseDTO<List<PassIdentityBaseDRO>> passIdentityResponse = this.listPassIdentityByTokenWithResponse(token);

        return passIdentityResponse.getData();
    }


    /**
     * 登录token获取登录用户身份信息
     * @param token 登录token
     * @return
     */
    public ResponseDTO<List<PassIdentityBaseDRO>> listPassIdentityByTokenWithResponse(String token){
        log.info("登录token获取登录用户身份信息入参：{}",token);
        ResponseDTO<List<PassIdentityBaseDRO>> passIdentityBaseResponse = tokenRemoteService.listPassIdentityByToken(token);
        if(!passIdentityBaseResponse.isSuccess()){
            log.error("登录token获取登录用户身份信息请求失败：{}",passIdentityBaseResponse.getMessage());
            throw new UacException(passIdentityBaseResponse.getMessage());
        }

        List<PassIdentityBaseDRO> passIdentityList = passIdentityBaseResponse.getData();
        log.info("登录token获取登录用户身份信息返回response：{}", JSON.toJSONString(passIdentityList));

        return passIdentityBaseResponse;
    }
}
